﻿using CPPEI.Coolzon.Designtime;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.DataPartLib.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for ConditionImageEditWindow.xaml
    /// </summary>
    public partial class ConditionImageEditWindow : Window
    {
        private ObservableCollection<ConditionImage> _ConditionImageList;
        public List<ConditionImage> RetConditionImageList { get; private set; }
        public ConditionImageEditWindow(List<ConditionImage> conditionImageList)
        {
            InitializeComponent();
            _ConditionImageList = new ObservableCollection<ConditionImage>();
            if (conditionImageList != null)
            {
                foreach (var item in conditionImageList)
                {
                    _ConditionImageList.Add(item);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _ctlImageDetailView.ItemsSource = _ConditionImageList;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RetConditionImageList = new List<ConditionImage>();
            if (_ConditionImageList != null && _ConditionImageList.Count > 0)
            {
                foreach (var item in _ConditionImageList)
                {
                    if (item.ImageSource == null)
                    {
                        CustomMessageBox.ShowWarning("请选择图片");
                        return;
                    }
                    RetConditionImageList.Add(item);
                }
            }
            DialogResult = true;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void cbPicName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            if (cb.SelectedItem != null && cb.SelectedIndex >= 0)
            {
                ImageRes imageRes = cb.SelectedItem as ImageRes;
                BitmapImage image = new BitmapImage();

                FileInfo file = new FileInfo(imageRes.FullPath);
                if (file != null && file.Exists)
                {
                    using (FileStream fileStream = File.Open(file.FullName, FileMode.Open, FileAccess.Read))
                    {
                        using (BinaryReader binReader = new BinaryReader(fileStream))
                        {
                            byte[] bytes = binReader.ReadBytes((int)file.Length);

                            image = new BitmapImage();
                            image.CacheOption = BitmapCacheOption.OnLoad;
                            image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                            image.BeginInit();
                            image.StreamSource = new MemoryStream(bytes);
                            image.EndInit();
                        }
                    }
                }

                (cb.Tag as ConditionImage).ImageSource = new ImageSourceFile(image, imageRes.FullPath);
            }
        }
        public static ImageRes[] GetImageList()
        {
            return ResPoolFactory.GetImageResPool().GetImages();
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            ConditionImage newItem = new ConditionImage();
            _ConditionImageList.Add(newItem);
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlImageDetailView.SelectedItem != null && _ctlImageDetailView.SelectedIndex >= 0)
            {
                _ConditionImageList.RemoveAt(_ctlImageDetailView.SelectedIndex);
            }
        }

        private void ComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox comboBox = (ComboBox)sender;
            if (comboBox != null && comboBox.Items.Count > 0)
            {
                ConditionImage currentConditionImage = (ConditionImage)comboBox.Tag;

                Uri fullPath = null;
                if (currentConditionImage != null && currentConditionImage.ImageSource != null)
                {
                    BitmapImage bitmapImage = (BitmapImage)currentConditionImage.ImageSource.ImageSource;
                    fullPath = new Uri(currentConditionImage.ImageSource.Path);
                }
                ImageRes selected = null;
                foreach (var item in comboBox.Items)
                {
                    if (fullPath != null && fullPath == new Uri((item as ImageRes).FullPath))
                    {
                        selected = (ImageRes)item;
                        break;
                    }
                }
                if (selected != null)
                {
                    comboBox.SelectedItem = selected;
                }
            }
        }

        private void Element_GotFocus(object sender, RoutedEventArgs e)
        {
            var aa = (sender as FrameworkElement).DataContext;
            _ctlImageDetailView.SelectedItem = aa;
        }
    }
}
